Matlab (5.1) graphics - an overview
Tomi Salminen, CSC 1998. An
introduction to Matlab graphics commands with examples and figures. To see all
options of a command, use the help command, e.g. help plot
Two dimensional graphs.
Elementary X-Y graphs.
plot - Linear plot.
loglog - Log-log scale plot.
semilogx - Semi-log scale plot.
semilogy - Semi-log scale plot.
polar - Polar coordinate plot.
plotyy - Graphs with y tick labels on the left and right.
Examples
|
x=0:pi/100:2*pi
y1=sin(x);
y2=cos(x);
plot(x,y1,'b-',x,y2,'r:');
|
|
theta=0:pi/100:2*pi;
rho=sin(theta*3);
polar(theta,rho,'go');
|
|
x=1:0.1:10;
y1=exp(x);
y2=exp(-x.^2);
plotyy(x,y1,x,y2,'semilogy');
grid on
|
Axis control.
axis - Control axis scaling and appearance.
zoom - Zoom in and out on a 2-D plot.
grid - Grid lines.
box - Axis box.
hold - Hold current graph.
axes - Create axes in arbitrary positions.
subplot - Create axes in tiled positions.
Examples
|
x=0:pi/100:2*pi;
y=sin(x);
subplot(2,1,1);
plot(x,y,'r');
axis([0 2*pi -0.7 0.7])
subplot(2,1,2)
plot(x,y,'b');
axis off
axes('Position',[0.6 0.2 0.3 0.2])
plot(x,y,'g')
grid on
|
Graph annotation.
legend - Graph legend.
title - Graph title.
xlabel - X-axis label.
ylabel - Y-axis label.
text - Text annotation.
gtext - Place text with mouse.
Examples
|
x=0:pi/100:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,'b',x,y2,'r');
legend('Sine','Cosine',0);
xlabel('X-axis');
ylabel('Y-axis');
title('Trigonometry rules OK');
text(4, 0.5,'Text at (4,0.5)');
|
Hardcopy and printing.
print - Print graph or SIMULINK system; or save graph to M-file.
printopt - Printer defaults.
orient - Set paper orientation.
The print command can output graphics in various Postscript
formats, including EPS. To create figure.eps, type print -deps
figure
Raster images (e.g., for the Web) are most conveniently saved using the
imwrite command, q.v.
Three dimensional graphs.
Elementary 3-D plots.
plot3 - Plot lines and points in 3-D space.
mesh - 3-D mesh surface.
surf - 3-D colored surface.
fill3 - Filled 3-D polygons.
Examples
|
t = 0:pi/50:5*pi;
plot3(sin(t),cos(t),t);
grid on
|
|
[X,Y] = meshgrid(-2:.4:2, -2:.4:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
mesh(X,Y,Z)
|
|
[X,Y] = meshgrid(-2:.4:2, -2:.4:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
surf(X,Y,Z)
|
Color control.
colormap - Color look-up table.
caxis - Pseudocolor axis scaling.
shading - Color shading mode.
hidden - Mesh hidden line removal mode.
brighten - Brighten or darken color map.
Examples
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
surf(X,Y,Z)
colormap(hot(15))
shading interp
|
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
surf(X,Y,Z)
colormap(hot(15))
shading interp
caxis([0.05 0.2])
|
Lighting.
surfl - 3-D shaded surface with lighting.
lighting - Lighting mode.
material - Material reflectance mode.
specular - Specular reflectance.
diffuse - Diffuse reflectance.
surfnorm - Surface normals.
Examples
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
surfl(X,Y,Z)
colormap(jet)
|
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
surf(X,Y,Z)
colormap(jet)
light
|
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
surf(X,Y,Z)
colormap(jet)
f = findobj('Type','surface');
set(f,'FaceLighting','phong');
material shiny
shading interp
light
|
Color maps.
hsv - Hue-saturation-value color map.
hot - Black-red-yellow-white color map.
gray - Linear gray-scale color map.
bone - Gray-scale with tinge of blue color map.
copper - Linear copper-tone color map.
pink - Pastel shades of pink color map.
white - All white color map.
flag - Alternating red, white, blue, and black color map.
lines - Color map with the line colors.
colorcube - Enhanced color-cube color map.
jet - Variant of HSV.
prism - Prism color map.
cool - Shades of cyan and magenta color map.
autumn - Shades of red and yellow color map.
spring - Shades of magenta and yellow color map.
winter - Shades of blue and green color map.
summer - Shades of green and yellow color map.
Axis control.
axis - Control axis scaling and appearance.
zoom - Zoom in and out on a 2-D plot.
grid - Grid lines.
box - Axis box.
hold - Hold current graph.
axes - Create axes in arbitrary positions.
subplot - Create axes in tiled positions.
Viewpoint control.
view - 3-D graph viewpoint specification.
viewmtx - View transformation matrix.
rotate3d - Interactively rotate view of 3-D plot.
Examples
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.^2 - Y.^2);
colormap(hsv)
subplot(1,2,1)
surf(X,Y,Z)
az=30;el=10;
view([az,el]);
subplot(1,2,2)
surf(X,Y,Z)
az=120;el=60;
view([az,el]);
|
Graph annotation.
title - Graph title.
xlabel - X-axis label.
ylabel - Y-axis label.
zlabel - Z-axis label.
colorbar - Display color bar (color scale).
text - Text annotation.
gtext - Mouse placement of text.
Examples
|
Z=peaks(20);
colormap(winter)
surf(Z)
colorbar
|
|
Z=peaks(20);
colormap(winter)
surf(Z);
h=colorbar('horiz');
set(h, 'PlotBoxAspectRatio',[10 0.4 1.0])
set(h, 'FontSize', 15);
|
Hardcopy and printing.
print - Print graph or SIMULINK system; or save graph to M-file.
printopt - Printer defaults.
orient - Set paper orientation.
Specialized graphs.
Specialized 2-D graphs.
area - Filled area plot.
bar - Bar graph.
barh - Horizontal bar graph.
bar3 - 3-D bar graph.
bar3h - Horizontal 3-D bar graph.
comet - Comet-like trajectory.
errorbar - Error bar plot.
ezplot - Easy to use function plotter.
feather - Feather plot.
fill - Filled 2-D polygons.
fplot - Plot function.
hist - Histogram.
pareto - Pareto chart.
pie - Pie chart.
pie3 - 3-D pie chart.
plotmatrix - Scatter plot matrix.
ribbon - Draw 2-D lines as ribbons in 3-D.
stem - Discrete sequence or "stem" plot.
stairs - Stairstep plot.
Examples
|
X=93:1:98;
Y=[1 2 3
4 5 4
2 3 4
3 6 5
1 2 4
2 3 2 ];
bar3(X,Y)
|
|
yn=randn(10000,1);
x=min(yn):0.2:max(yn);
hist(yn,x);
|
|
x = 1:10;
y = 100*rand(10,1);
e = sqrt(y);
errorbar(x,y,e)
|
|
ezplot('besselj(0,x)')
|
|
fplot('erf(x)', [-pi pi])
|
Contour and 2-1/2 D graphs.
contour - Contour plot.
contourf - Filled contour plot.
contour3 - 3-D Contour plot.
clabel - Contour plot elevation labels.
pcolor - Pseudocolor (checkerboard) plot.
quiver - Quiver plot.
voronoi - Voronoi diagram.
Examples
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.*X - Y.*Y);
[fx,fy] = gradient(Z);
contour(X,Y,Z,10)
hold on
quiver(X,Y,fx,fy);
hold off
|
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.*X - Y.*Y);
[fx,fy] = gradient(Z);
[cs,h]=contourf(X,Y,Z,10);
clabel(cs,h);
|
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.*X - Y.*Y);
contour3(X,Y,Z,20);
|
Specialized 3-D graphs.
comet3 - 3-D comet-like trajectories.
meshc - Combination mesh/contour plot.
meshz - 3-D mesh with curtain.
stem3 - 3-D stem plot.
quiver3 - 3-D quiver plot.
slice - Volumetric slice plot.
surfc - Combination surf/contour plot.
trisurf - Triangular surface plot.
trimesh - Triangular mesh plot.
waterfall - Waterfall plot.
Examples
slice is the
only command available in matlab for the visualization of volume data.
Matlab cannot create isosurfaces or do direct colume rendering. If you
need to examine volumes, I suggest you look into more advanced
visualization tools such as AVS and FUNCS.
|
[x,y,z] = meshgrid(-2:.2:2, -2:.25:2, -2:.16:2);
v = x .* exp(-x.^2 - y.^2 - z.^2);
slice(x,y,z,v,1.5, 1, 0)
|
|
[X,Y] = meshgrid(-2:.2:2, -2:.2:2);
Z = sin(X).* sin(Y).* ...
exp(-X.*X - Y.*Y);
waterfall(X,Y,Z);
|
Images display and file I/O.
image - Display image.
imagesc - Scale data and display as image.
colormap - Color look-up table.
gray - Linear gray-scale color map.
contrast - Gray scale color map to enhance image contrast.
brighten - Brighten or darken color map.
colorbar - Display color bar (color scale).
imread - Read image from graphics file.
imwrite - Write image to graphics file.
imfinfo - Information about graphics file.
To save your figure exactly as you see it on the screen, use the following
commands to save it in TIFF-format (note: imwrite has a bug that affects
24-bit graphics):
[X,map] = capture(gcf);
imwrite(X,map,'figure.tif')
Type help imwrite to see other parameters for imwrite.
Examples
|
Z=peaks(50);
imagesc(Z);
colorbar;
|
Movies and animation.
capture - Screen capture of current figure.
moviein - Initialize movie frame memory.
getframe - Get movie frame.
movie - Play recorded movie frames.
qtwrite - Translate movie into QuickTime format (Macintosh only).
rotate - Rotate object about specified orgin and direction.
frame2im - Convert movie frame to indexed image.
im2frame - Convert index image into movie format.
To create a movie to run within matlab, try the following:
M=moviein(10);
set(gca, 'NextPlot','replacechildren')
for
j=1:10
surf(peaks(10))
view([j*10 30])
M(:,j) = getframe;
end
movie(M,4);
A more robust method is to save each frame on disk, using the
imwrite command, and then use a separate program (such as
dmconvert on our SGIs) to compose a MPEG or a Quicktime animation, for
example.
Color related functions.
spinmap - Spin color map.
rgbplot - Plot color map.
colstyle - Parse color and style from string.
Solid modeling.
cylinder - Generate cylinder.
sphere - Generate sphere.
patch - Create patch.
Examples
|
[X,Y,Z]=sphere(20);
C=rand(21,21);
surf(X,Y,Z,C)
axis equal
shading interp
light
|
Handle Graphics.
Examples
|
f=figure('Position',[100 100 300 250]);
set(f, 'Color', 'black')
s=surf(peaks(30));
axis tight
c=colorbar;
set(c,'XColor','white','YColor','white');
set(c,'FontSize',10,'FontWeight','bold');
set(gca,'LineWidth',3);
set(gca,'Projection','perspective')
set(gca,'FontSize',14);
set(gca,'Color',[0.1 0.1 0.1]);
set(gca,'XColor','white',...
'YColor','white',...
'ZColor','white');
o = findobj('Type','surface');
set(o,'FaceLighting','phong')
shading interp
light('Position',[0 -1 2])
|
Figure window creation and control.
figure - Create figure window.
gcf - Get handle to current figure.
clf - Clear current figure.
shg - Show graph window.
close - Close figure.
refresh - Refresh figure.
Axis creation and control.
subplot - Create axes in tiled positions.
axes - Create axes in arbitrary positions.
gca - Get handle to current axes.
cla - Clear current axes.
axis - Control axis scaling and appearance.
box - Axis box.
caxis - Control pseudocolor axis scaling.
hold - Hold current graph.
ishold - Return hold state.
Handle Graphics objects.
figure - Create figure window.
axes - Create axes.
line - Create line.
text - Create text.
patch - Create patch.
surface - Create surface.
image - Create image.
light - Create light.
uicontrol - Create user interface control.
uimenu - Create user interface menu.
Handle Graphics operations.
set - Set object properties.
get - Get object properties.
reset - Reset object properties.
delete - Delete object.
gco - Get handle to current object.
gcbo - Get handle to current callback object.
gcbf - Get handle to current callback figure.
drawnow - Flush pending graphics events.
findobj - Find objects with specified property values.
copyobj - Make copy of graphics object and its children.